// pages/user/user.js
const db=wx.cloud.database()
Page({
  data: {
    exist:false,
    openid:"",
    url:"",
    nickName:"",
    experience:10,
    xingren:"",
    days:"",
    signDay:"0",
    reciteDay:"0",
    recite:[
      {data_index:0,number:0,pre_index:0}
    ],
    test:[],
    realTest:[],
    star:[],
    msg:[],
    user_list: [
      { text: '账号管理', img: '../img/bitbug_favicon.ico'},
      { text: '我的杏仁', img: '../img/tabbar/icon-checkbox-click.png' },
      { text: '我的收藏', img: '../img/tabbar/icon-checkbox-click.png' },
      { text: '我的学习', img: '../img/tabbar/icon-checkbox-click.png' },
      { text: '我的消息', img: '../img/tabbar/icon-checkbox-click.png' },
      { text: '反馈意见', img: '../img/tabbar/icon-checkbox-click.png' },
    ],
    header_list:[
      {title:'完成学习',container:'12'},
      {title:'完成课程',container:'5'},
      {title:'收藏',container:'9'}
    ]
  },
  onLoad(){

    let that=this
    if (wx.getStorageSync('nickName')&&wx.getStorageSync('url')) {
      that.setData({
        exist:true,
        nickName:wx.getStorageSync('nickName'),
        url:wx.getStorageSync('url'),
        openid:wx.getStorageSync('openid')
      })
    }
    else{
      wx.cloud.callFunction({
      name:"getOpenId",
      complete: res => {
        console.log(res)
        db.collection("userList").where({
          openid:res.result.openid   //进行筛选
        }).get({
          success:res=>{
            console.log(res.data)
            if(res.data.length==0){
              that.setData({
                exist:false
              })
            }
            else
            {
              that.setData({
                exist:true,
                openid:res.data[0].openid,
                nickName:res.data[0].nickName,
                url:res.data[0].avatarUrl,
                experience:res.data[0].experience,
                xingren:res.data[0].xingren,
                recite:res.data[0].recite,
                test:res.data[0].test,
                realTest:res.data[0].realTest,
                star:res.data[0].star,
                msg:res.data[0].msg,
              })
                wx.setStorageSync('nickName', that.data.nickName)
                wx.setStorageSync('url', that.data.url)
                wx.setStorageSync('openid', that.data.openid)
            }
          }
        })
        
        if (that.data.exist==false) {
          console.log(res.result.openid)
          that.setData({
          openid:res.result.openid
          })
        }
      }
    })
  }
},
  bindgetuserinfoFun(e)
  {
    console.log(e);
    let that=this
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.login({
            success: (res) => {
              console.log('wechatCode', res.code);
              // that.wxLoginOpt(res.code, e.detail.userInfo)
              that.setData({
                url:e.detail.userInfo.avatarUrl,
                nickName:e.detail.userInfo.nickName
              })
              if(that.data.exist==false)
              {
                wx.cloud.callFunction({
                  name:"updata",
                  data:{
                    action:"userList",
                    url:e.detail.userInfo.avatarUrl,
                    nickName:e.detail.userInfo.nickName,
                    openid:that.data.openid,
                    experience:that.data.experience,
                    xingren:that.data.xingren,
                    recite:that.data.recite,
                    test:that.data.test,
                    days:that.data.days,
                    signDay:that.data.signDay,
                    reciteDay:that.data.reciteDay,
                    realTest:that.data.realTest,
                    star:that.data.star,
                    msg:that.data.msg,
                  }
              })
              .then(res=>{
                console.log('插入成功',res)
                console.log(that.data)
                that.setData({
                    exist:true
                })
                wx.setStorageSync('nickName', that.data.nickName)
                wx.setStorageSync('url', that.data.url)
                wx.setStorageSync('openid', that.data.openid)
              })
              .catch(res=>{
                console.log('插入失败',res)
              })
              }
              else
              {
                wx.showToast({
                  title:"用户已存在",
                  icon:"none"
                })
              }
            }
          })
        }else {
          wx.authorize({
            scope:'scope.userInfo',
            success() {
 
            }
          })
        }
      }
    })
    // that.setData({
    //   url:e.detail.userInfo.avatarUrl,
    //   nickName:e.detail.userInfo.nickName
    // })
    // if(that.data.exist==false)
    // {
    //   wx.cloud.callFunction({
    //     name:"updata",
    //     data:{
    //       action:"userList",
    //       url:e.detail.userInfo.avatarUrl,
    //       nickName:e.detail.userInfo.nickName,
    //       openid:this.data.openid,
    //       experience:that.data.experience,
    //       xingren:that.data.xingren,
    //       recite:that.data.recite,
    //       test:that.data.test,
    //       realTest:that.data.realTest,
    //       star:that.data.star,
    //       msg:that.data.msg,
    //     }
    // })
    // .then(res=>{
    //   console.log('插入成功',res)
    //   that.setData({
    //       exist:true
    //   })
    //   wx.setStorageSync('nickName', that.data.nickName)
    //   wx.setStorageSync('url', that.data.url)
    //   wx.setStorageSync('openid', that.data.openid)
    // })
    // .catch(res=>{
    //   console.log('插入失败',res)
    // })
    // }
    // else
    // {
    //   wx.showToast({
    //     title:"用户已存在",
    //     icon:"none"
    //   })
    // }     
  },
  toPage(e){
    var that=this
    var url=that.data.url
    var nickName=that.data.nickName
    var text=e.target.dataset.text
    console.log(e.target.dataset)
    if (text=="账号管理") {
      wx.navigateTo({
        url: '../account/account?url='+url+"&nickName="+nickName,
      })
    }
    else if(text=="反馈意见"){
      wx.navigateTo({
        url: '../back/back'
      })
    }
    else if (text=="我的杏仁") {
      wx.navigateTo({
        url: '../money/money?xingren='+this.data.xingren
      })
    }
    else if (text=="我的学习") {
      wx.navigateTo({
        url: '../myStudy/myStudy'
      })
    }
    else if (text=="我的收藏") {
      wx.navigateTo({
        url: '../myStar/myStar'
      })
    }
  }
})